
#include "Orange.h"

class TestLayer : public Orange::Layer
{
public:
    TestLayer() : Layer("TestLayer"), m_Camera(-1.0f, 1.0f, -1.0f, 1.0f)
    {
    }
    void OnAttach() override
    {

        // 顶点缓冲
        // 绑定顶点
        m_VertexArray = Orange::VertexArray::Create();

        // 顶点
        float vertexs[4 * 7] = {
            -0.5f, -0.5f, 0.0f, 0.8, 0.3, 0.2, 1.0,
            0.5f, -0.5f, 0.0f, 0.8, 0.3, 0.2, 1.0,
            0.5f, 0.5f, 0.0f, 0.8, 0.3, 0.2, 1.0,
            -0.5f, 0.5f, 0.0f, 0.8, 0.3, 0.2, 1.0};

        Orange::Ref<Orange::VertexBuffer> trangleVB;
        trangleVB = Orange::VertexBuffer::Create(vertexs, sizeof(vertexs));

        Orange::ShaderLayout layout = {
            {Orange::ShaderDataType::FLoat3, "a_position"},
            {Orange::ShaderDataType::FLoat4, "a_color"},
        };

        trangleVB->SetLayout(layout);

        m_VertexArray->AddVertexBuffer(trangleVB);

        //  索引缓冲
        uint32_t indexes[6] = {
            0, 1, 2,
            2, 3, 0};
        Orange::Ref<Orange::IndexBuffer> trangleIB;
        trangleIB = Orange::IndexBuffer::Create(indexes, sizeof(indexes) / sizeof(uint32_t));

        m_VertexArray->SetIndexBuffer(trangleIB);

        // opengl
        m_Shader = Orange::Shader::Create("D:/learn/engine/Sandbox/assets/shader/flatColor.glsl");
        m_Shader->Bind();
    }

    void OnDetach() override{

    };

    void OnUpdate() override
    {

        {
            // Render Pre
            Orange::RendererCommand::SetClearColor({0.0f, 0.0f, 0.0f, 1.0f});
            Orange::RendererCommand::Clear();
        }

        if (Orange::Input::IsKeyPressed(RPG_KEY_A))
        {
            m_CameraPosition.x -= 0.05f;
        }
        else if (Orange::Input::IsKeyPressed(RPG_KEY_D))
        {
            m_CameraPosition.x += 0.05f;
        }
        else if (Orange::Input::IsKeyPressed(RPG_KEY_W))
        {
            m_CameraPosition.y += 0.05f;
        }
        else if (Orange::Input::IsKeyPressed(RPG_KEY_S))
        {
            m_CameraPosition.y -= 0.05f;
        }

        Orange::Renderer::StartScene();
        m_Camera.SetPosition(m_CameraPosition);
        m_Shader->SetUniformMat4("viewPreject", m_Camera.GetViewPrejectMatrix());
        Orange::Renderer::Submit(m_Shader, m_VertexArray);

        Orange::Renderer::EndScene();
    };
    void OnImGuiRender() override{

    };

private:
    // Renderer
    Orange::Ref<Orange::VertexArray> m_VertexArray;
    Orange::Ref<Orange::Shader> m_Shader;
    Orange::OrthogonaCamera m_Camera;
    glm::vec3 m_CameraPosition = {0.0f, 0.0f, 0.0f};
};

class SandboxApplication : public Orange::Application
{
public:
    SandboxApplication()
    {
        PushOverLayer(new TestLayer());
    }

    ~SandboxApplication()
    {
    }
};

Orange::Application *Orange::CreateApplication()
{
    return new SandboxApplication();
}
